package cn.jhc.db;

import cn.jhc.beans.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    private static final String LOGIN_SQL = "SELECT id, username, pwd, email, upload_file, disk_file FROM survey_user WHERE username = ? AND pwd = ?";
    
    public static User login(Connection connection, String username, String password) {
        User user = null;
        try {
            PreparedStatement statement = connection.prepareStatement(LOGIN_SQL);
            statement.setString(1, username);
            statement.setString(2, password);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("pwd"));
                user.setEmail(resultSet.getString("email"));
                user.setUploadFile(resultSet.getString("upload_file"));
                user.setDiskFile(resultSet.getString("disk_file"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
    
    public static boolean register(Connection connection, User user) {
        //TODO
        return false;
    }
}
